package prototype;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;

public class DailyUsageGraph {

    private static JFreeChart chart;
    private JButton schedule;
    private JButton btnWeeklyUsage;
    private JComboBox days;

    public DailyUsageGraph() {

        String[] dayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                "Saturday", "Sunday"};

        final XYSeries powerplant = new XYSeries("Powerplant");
        powerplant.add(0.0, 50.0);
        powerplant.add(24.0, 50.0);

        final XYSeries house = new XYSeries("House");

        house.add(0.0, 30.0);
        house.add(2.0, 50.0);
        house.add(4.0, 60.0);
        house.add(6.0, 65.0);
        house.add(8.0, 20.0);
        house.add(10.0, 15.0);
        house.add(12.0, 35.0);
        house.add(14.0, 30.0);
        house.add(16.0, 45.0);
        house.add(18.0, 50.0);
        house.add(20.0, 30.0);
        house.add(22.0, 10.0);
        house.add(24.0, 5.0);

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(powerplant);
        dataset.addSeries(house);

        chart = ChartFactory.createXYLineChart("Daily Usage", "Hours",
                "Usage", dataset, PlotOrientation.VERTICAL, true, true, false);


        schedule = new JButton("Schedule");
        btnWeeklyUsage = new JButton("Weekly Usage");
        days = new JComboBox(dayNames);
    }

    public ChartPanel createGraph() {

        final ChartPanel chartPanel = new ChartPanel(chart);
        GridBagLayout gbl_chartPanel = new GridBagLayout();
        gbl_chartPanel.columnWidths = new int[]{50, 50, 250, 200, 150, 0};
        gbl_chartPanel.rowHeights = new int[]{538, 0, 0, 23, 0};
        gbl_chartPanel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_chartPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        chartPanel.setLayout(gbl_chartPanel);

        GridBagConstraints gbc_schedule = new GridBagConstraints();
        gbc_schedule.insets = new Insets(0, 0, 5, 5);
        gbc_schedule.anchor = GridBagConstraints.WEST;
        gbc_schedule.gridx = 4;
        gbc_schedule.gridy = 2;
        chartPanel.add(schedule, gbc_schedule);

        GridBagConstraints gbc_btnWeeklyUsage = new GridBagConstraints();
        gbc_btnWeeklyUsage.insets = new Insets(0, 0, 5, 5);
        gbc_btnWeeklyUsage.anchor = GridBagConstraints.EAST;
        gbc_btnWeeklyUsage.gridx = 3;
        gbc_btnWeeklyUsage.gridy = 2;
        chartPanel.add(btnWeeklyUsage, gbc_btnWeeklyUsage);

        GridBagConstraints gbc_days = new GridBagConstraints();
        gbc_days.insets = new Insets(0, 0, 5, 5);
        gbc_days.anchor = GridBagConstraints.EAST;
        gbc_days.gridx = 1;
        gbc_days.gridy = 2;
        chartPanel.add(days, gbc_days);

        return chartPanel;
    }

    public JButton getScheduleButton() {
        return schedule;
    }

    public JButton getBtnWeeklyUsage() {
        return btnWeeklyUsage;
    }

    public JComboBox getDays() { return days; }
}
